package com.tongyi.lizhishop.activitys;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.tongyi.lizhishop.views.shinkbutton.ShrinkButton;

import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.api.UserApi;
import com.tongyi.lizhishop.beans.CommonResonseBean;
import com.tongyi.lizhishop.common.BaseActivity;
import com.tongyi.lizhishop.managers.APIServiceManager;

import java.util.HashSet;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.forgetPassword)
    TextView forgetPassword;
    @BindView(R.id.login)
    ShrinkButton openAlive;
    @BindView(R.id.activity_login)
    LinearLayout activityLogin;
    @BindView(R.id.iphone)
    EditText iphone;
    @BindView(R.id.password)
    EditText password;
    private String iphoneText;
    private String pwdText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        openAlive.setEnabled(false);
        iphone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                iphoneText = s.toString();
                checkBtnCanClick();
            }


        });
        password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                pwdText = s.toString();
                checkBtnCanClick();
            }
        });
    }

    private void checkBtnCanClick() {
        if (!TextUtils.isEmpty(pwdText) && !TextUtils.isEmpty(iphoneText)) {
            openAlive.setEnabled(true);
        } else {
            openAlive.setEnabled(false);
        }

    }

    @OnClick({R.id.forgetPassword, R.id.login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.forgetPassword:
                ActivityUtils.startActivity(this, FindPasswordActivity.class);
                break;
            case R.id.login:
                APIServiceManager.getInstance()
                        .create(UserApi.class)
                        .login(iphoneText, pwdText)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(new Consumer<CommonResonseBean>() {
                            @Override
                            public void accept(CommonResonseBean commonResonseBean) throws Exception {
                                if ("succ".equals(commonResonseBean.getRe())) {
                                    //登录成功;
                                    JPushInterface.setAlias(LoginActivity.this, 1, commonResonseBean.getArray().getSel_id());
                                    HashSet<String> set = new HashSet<>();
                                    set.add(commonResonseBean.getArray().getSel_id());
                                    JPushInterface.setTags(LoginActivity.this, 1, set);
                                    ToastUtils.showShort("登录成功");
                                    SPUtils.getInstance().put("iphone", iphoneText);
                                    SPUtils.getInstance().put("pwd", pwdText);
                                    SPUtils.getInstance().put("userid", commonResonseBean.getArray().getSel_id());
                                    ActivityUtils.startActivity(LoginActivity.this, MainActivity.class);
                                    finish();
                                } else {
                                    ToastUtils.showShort("您输入的账户或密码错误");
                                    openAlive.reset();
                                }
                                LogUtils.e(commonResonseBean);
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                ToastUtils.showShort("您输入的账户或密码错误");
                                openAlive.reset();
                                //   LogUtils.e(throwable.toString());
                            }
                        });


                break;
        }
    }

}
